package com.dhcc.sdc.base.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.base.dao.SdcBaseAssessmenjudgementMapper;
import com.dhcc.sdc.base.dao.SdcBaseAssessmentMapper;
import com.dhcc.sdc.base.dao.SdcBaseAssessmentNodeMapper;
import com.dhcc.sdc.base.entity.SdcBaseAssessmenjudgementEntity;
import com.dhcc.sdc.base.entity.SdcBaseAssessmentEntity;
import com.dhcc.sdc.base.entity.SdcBaseAssessmentNodeEntity;
import com.dhcc.sdc.base.service.ISdcBaseAssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 表数据服务层接口
 * 
 * @ClassName: SdcBaseAssessmentServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-09-14 16:35:21
 */
@Service("sdcBaseAssessmentService")
public class SdcBaseAssessmentServiceImpl extends BaseServiceImpl<SdcBaseAssessmentMapper, SdcBaseAssessmentEntity> implements ISdcBaseAssessmentService {

	@Autowired
	private SdcBaseAssessmenjudgementMapper sdcBaseAssessmenjudgementMapper;
	@Autowired
	private SdcBaseAssessmentNodeMapper sdcBaseAssessmentNodeMapper;

	@Override
	public List<SdcBaseAssessmentEntity> page(Page<SdcBaseAssessmentEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<SdcBaseAssessmentEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public List<ZTreeNode> tree() {
		List<ZTreeNode> tree = this.baseMapper.tree();
		tree.add(ZTreeNode.createRoot("评估/评价表"));
		return tree;
	}

	@Override
	@Transactional
	public void addDict(SdcBaseAssessmentEntity dict, String levelInfos, String evalNodes) {
		// 判断有没有该评估表
		List<SdcBaseAssessmentEntity> dicts = baseMapper
				.selectList(new EntityWrapper<SdcBaseAssessmentEntity>().eq("code", dict.getCode()).and().eq("type", dict.getType()));
		if (dicts != null && dicts.size() > 0) {
			throw new BizException(BizExceptionEnum.DICT_EXISTED);
		}

		// 添加字典
		SdcBaseAssessmentEntity newAsmt = new SdcBaseAssessmentEntity();
		newAsmt.setCode(dict.getCode());
		newAsmt.setDescription(dict.getDescription());
		newAsmt.setCdssEvalName(dict.getCdssEvalName());
		newAsmt.setType(dict.getType());
		newAsmt.setShowStyle(dict.getShowStyle());
		newAsmt.setIsActive(dict.getIsActive());
		this.baseMapper.insert(newAsmt);

		// dicLevelInfo
		JSONArray infos = JSONArray.parseArray(levelInfos);
		// 添加字典条目
		for (Object info : infos) {
			JSONObject levelInfo = JSONObject.parseObject(info.toString());
			SdcBaseAssessmenjudgementEntity judgeEntity = new SdcBaseAssessmenjudgementEntity();
			judgeEntity.setAssessmentId(newAsmt.getId());
			judgeEntity.setLowerLimit(levelInfo.getString("lowerLimit"));
			judgeEntity.setUpperLimit(levelInfo.getString("upperLimit"));
			judgeEntity.setResult(levelInfo.getString("result"));
			judgeEntity.insert();
		}
		//添加节点
		if(CommonUtil.isNotEmpty(evalNodes)){
			JSONArray evalNos = JSONArray.parseArray(evalNodes);
			for (Object info : evalNos) {
				JSONObject nodeInfo = JSONObject.parseObject(info.toString());
				SdcBaseAssessmentNodeEntity nodeEntity = new SdcBaseAssessmentNodeEntity();
				nodeEntity.setAssessmentId(newAsmt.getId());
				nodeEntity.setNode(nodeInfo.getString("result"));
				nodeEntity.setCreateDate(new Date());
				nodeEntity.insert();
			}
		}
	}

	@Override
	@Transactional
	public void editDict(SdcBaseAssessmentEntity sdcBaseAssessment, String levelInfos, String evalNodeInfos) {
		SdcBaseAssessmentEntity old = this.baseMapper.selectById(sdcBaseAssessment.getId());

		old.setCode(sdcBaseAssessment.getCode());
		old.setDescription(sdcBaseAssessment.getDescription());

		old.setType(sdcBaseAssessment.getType());
		old.setCdssEvalName(sdcBaseAssessment.getCdssEvalName());
		old.setNodeDictTypeId(sdcBaseAssessment.getNodeDictTypeId());
		old.setShowStyle(sdcBaseAssessment.getShowStyle());
		old.setIsActive(sdcBaseAssessment.getIsActive());
		old.updateAllColumnById();

		// 删除旧的分数等级信息
		Map param = new HashMap();
		param.put("assessment_id", sdcBaseAssessment.getId());
		this.sdcBaseAssessmenjudgementMapper.deleteByMap(param);
		// 添加字典条目
		JSONArray infos = JSONArray.parseArray(levelInfos);
		for (Object info : infos) {
			JSONObject levelInfo = JSONObject.parseObject(info.toString());
			SdcBaseAssessmenjudgementEntity judgeEntity = new SdcBaseAssessmenjudgementEntity();
			judgeEntity.setAssessmentId(sdcBaseAssessment.getId());
			judgeEntity.setLowerLimit(levelInfo.getString("lowerLimit"));
			judgeEntity.setUpperLimit(levelInfo.getString("upperLimit"));
			judgeEntity.setResult(levelInfo.getString("result"));
			judgeEntity.insert();
		}
		//评估节点信息
		//删除旧的节点信息
		Map param1 = new HashMap();
		param1.put("assessment_id", sdcBaseAssessment.getId());
		this.sdcBaseAssessmentNodeMapper.deleteByMap(param1);
		//增加新的节点信息
		if(CommonUtil.isNotEmpty(evalNodeInfos)){
			JSONArray evalNos = JSONArray.parseArray(evalNodeInfos);
			for (Object info : evalNos) {
				JSONObject nodeInfo = JSONObject.parseObject(info.toString());
				SdcBaseAssessmentNodeEntity nodeEntity = new SdcBaseAssessmentNodeEntity();
				nodeEntity.setAssessmentId(sdcBaseAssessment.getId());
				nodeEntity.setNode(nodeInfo.getString("result"));
				nodeEntity.setCreateDate(new Date());
				nodeEntity.insert();
			}
		}

	}

	@Override
	@Transactional
	public void delteDict(Long sdcBaseAssessmentId) {
		// 删除这个评估表的分级信息
		Wrapper<SdcBaseAssessmenjudgementEntity> dictEntityWrapper = new EntityWrapper<>();
		dictEntityWrapper = dictEntityWrapper.eq("assessment_id", sdcBaseAssessmentId);
		sdcBaseAssessmenjudgementMapper.delete(dictEntityWrapper);

		// 删除这个评估表
		baseMapper.deleteById(sdcBaseAssessmentId);
	}

	@Override
	public JSONArray getJSONArray() {
		List<SdcBaseAssessmentEntity> sdcBaseAssessmentEntities = this.baseMapper.selectList(null);
		JSONArray officeArry = new JSONArray();
		for(SdcBaseAssessmentEntity baseAssessmentEntity : sdcBaseAssessmentEntities){
			JSONObject officeObj = new JSONObject();
			officeObj.put("value", baseAssessmentEntity.getId() + "");
			officeObj.put("text", baseAssessmentEntity.getDescription());
			officeArry.add(officeObj);
		}
		return officeArry;
	}

}